import Vue from 'vue'



//返回
function customback(){
	// history.go(-1); 
	const pages = getCurrentPages()
		if (pages.length === 1) {
			if (typeof params === 'number') {
				history.go(-params);
				
			} else {
				history.back()
			}
		} else {
			uni.navigateBack();
		}
}
//字符串截取
function getCaption(strings) {
	var strLength = strings.length;
	var shortName = strings.substring(0, 5);
	var shortName2 = strings.substring(strLength - 5, strLength);
	let str = `${shortName}...${shortName2}`;
	return str
}

function machfto(unm,sun){
	let Numberunm
	if(typeof(unm) == 'string'){
		Numberunm = Math.floor(Number(unm)*1000)/1000
	}else{
		Numberunm = Math.floor(unm*1000)/1000
	}
	return Numberunm
}
Vue.prototype.machfto = machfto;
Vue.prototype.customback = customback;
Vue.prototype.getCaption = getCaption;

// 解决小数精度丢失
// 除法
function accDiv(arg1, arg2) {
    let t1, t2;
    t1 = getDecimalLength(arg1)
    t2 = getDecimalLength(arg2)
    if(t1 - t2 > 0) {
        return (getNum(arg1) / getNum(arg2)) / Math.pow(10, t1 - t2)
    } else {
        return (getNum(arg1) / getNum(arg2)) * Math.pow(10, t2 - t1)
    }
}
function getDecimalLength(arg = ''){
    try {
        return arg.toString().split(".")[1].length
    } catch (e) {
        return 0
    }
}
function getNum(arg = ''){
    return Number(arg.toString().replace(".", ""))
}
//乘法
function accMul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}
//加法
function accAdd(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2))
    return (arg1 * m + arg2 * m) / m
}
//减法
function Subtr(arg1, arg2) {
    var r1, r2, m, n;
    try {r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
//解决科学计数
function number2string(num) {
	num = String(num)
	var eSplit = num.split('e')
	var e = +(eSplit[1] || 0)
	var pad = '0'.repeat(500)
	var numSplit = eSplit[0].split('.')
	var str
	var numSplit1 = numSplit[1] || ''
	if (e < 0) {
		str = ('0.' + (pad + numSplit[0]).slice(e) + numSplit1)
	} else {
		str = (numSplit[0] + (numSplit1 + pad).slice(0, e) + '.' + numSplit1.slice(e))
	}
	return str.replace(/\.?0*$/, '').replace(/^0*/, '').replace(/^\./, '0.') || '0'
}
// 格式化日对象
function getNowDate() {
  var date = new Date();
  var sign2 = ":";
  var year = date.getFullYear() // 年
  var month = date.getMonth() + 1; // 月
  var day = date.getDate(); // 日
  var hour = date.getHours(); // 时
  var minutes = date.getMinutes(); // 分
  var seconds = date.getSeconds() //秒
  var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
  var week = weekArr[date.getDay()];
  // 给一位数的数据前面加 “0”
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
}
export {
  accDiv,
  accMul,
  accAdd,
  Subtr,
  getCaption,
  getNowDate,
  number2string
}